package com.jse.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Created by Wizzer.cn on 2015/7/2.
 */
public class Cookies {
	
	
	/**
	 * 设置 Cookie（生成时间为1天）
	 *
	 * @param name
	 *            名称
	 * @param value
	 *            值
	 */
	public static void setCookie(HttpServletResponse response,String domain, String name, String value) {
		setCookie(response,domain, name, value, 60 * 60 * 24);
	}

	/**
	 * 设置 Cookie
	 *
	 * @param name
	 *            名称
	 * @param value
	 *            值
	 * @param path
	 *            路径
	 */
	public static void setCookie(HttpServletResponse response,String domain, String name, String value, String path) {
		setCookie(response, name, value,domain, path, 60 * 60 * 24);
	}

	/**
	 * 设置 Cookie
	 *
	 * @param name
	 *            名称
	 * @param value
	 *            值
	 * @param maxAge
	 *            生存时间（单位秒）
	 */
	public static void setCookie(HttpServletResponse response, String name, String value,String domain, int maxAge) {
		setCookie(response, name, value,domain, "/", maxAge);
	}

	/**
	 * 设置 Cookie
	 *
	 * @param name
	 *            名称
	 * @param value
	 *            值
	 * @param maxAge
	 *            生存时间（单位秒）
	 * @param path
	 *            路径
	 */
	public static void setCookie(HttpServletResponse response, String name, String value,String domain, String path, int maxAge) {
		Cookie cookie = new Cookie(name, null);
		cookie.setDomain(domain);
		cookie.setPath(path);
		cookie.setMaxAge(maxAge);
		try {
			cookie.setValue(URLEncoder.encode(value, "utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		response.addCookie(cookie);
	}

	/**
	 * 获得指定Cookie的值
	 *
	 * @param name
	 *            名称
	 * @return 值
	 */
	public static String getCookie(HttpServletRequest request, String name) {
		return getCookie(request, null, name, false);
	}

	/**
	 * 获得指定Cookie的值，并删除。
	 *
	 * @param name
	 *            名称
	 * @return 值
	 */
	public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name) {
		return getCookie(request, response, name, true);
	}

	/**
	 * 获得指定Cookie的值
	 *
	 * @param request
	 *            请求对象
	 * @param response
	 *            响应对象
	 * @param name
	 *            名字
	 * @param isRemove
	 *            是否移除
	 * @return 值
	 */
	public static String getCookie(HttpServletRequest request, HttpServletResponse response, String name,
			boolean isRemove) {
		String value = null;
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals(name)) {
					try {
						value = URLDecoder.decode(cookie.getValue(), "utf-8");
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					}
					if (isRemove) {
						cookie.setMaxAge(0);
						response.addCookie(cookie);
					}
				}
			}
		}
		return value;
	}

	/**
	 * 获得Cookie集合
	 *
	 * @param request
	 *            请求对象
	 * @param response
	 *            响应对象
	 * @return 值
	 */
	public static Map<String, String> getCookie(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> value = new HashMap<String, String>();
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				try {
					value.put(cookie.getName(), URLDecoder.decode(cookie.getValue(), "utf-8"));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return value;
	}

	/**
	 * 清空cookie
	 */
	public static void clear(HttpServletRequest request,HttpServletResponse response) {
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				cookie.setMaxAge(0);
				response.addCookie(cookie);
			}
		}
	}
	
	public static void del(HttpServletRequest request,HttpServletResponse response,String name) {
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if(cookie.getName().equals(name)) {
					cookie.setMaxAge(0);
					response.addCookie(cookie);
				}
				
			}
		}
	}

}
